package com.itheima.health.rubbishimgdelete;


import com.itheima.health.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Set;

@Component
public class RubbishImgDelete {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private QiniuUtils qiniuUtils;

    @Scheduled(cron = "0 0 2 * * ?")
    public void deleteImg(){
        Set<String> updateimg = redisTemplate.opsForSet().members("uplodeimg");
        Set<String> insertimg = redisTemplate.opsForSet().members("insertimg");
        int count = 0;
        if(insertimg.size()!=0 || updateimg.size()!=0){
            for (String s : updateimg) {
                String[] s2 = s.split("_");
                long currentTimeMillis = System.currentTimeMillis();
                long scurrentTimeMillis = Long.parseLong(s2[0]);
                long cha = currentTimeMillis-scurrentTimeMillis;
                for (String s1 : insertimg) {
                    if(s1==s){
                       count++;
                    }
                }
                if(count==0 && cha>1800000){
                    qiniuUtils.deleteFileFromQiniu(s);
                    redisTemplate.opsForSet().remove("uplodeimg",s);
                }
            }
        }
    }
}
